Skip to content

Offer the null check alone where a 'mutating' call cannot use a '??' fallback - #321

Merged
AlexDicy merged 13 commits into
mainfrom
agent/156-optional-chain-mutating-fallback
Dec 1, 2026
Merged

AlexDicy merged 13 commits into
mainfrom
agent/156-optional-chain-mutating-fallback

Conversation

@DicyAgent

@DicyAgent DicyAgent commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #156

local?.plain.bump() is refused for a null local with a message that offered a ?? fallback, which the call then refuses because a fallback value is no place to change. The refusal now leaves the fallback out where the change cannot land in one, and says why:

Cannot call a member of 'Counter?' because it may be null here. 'local?.plain' is null whenever
'local' is null. 'bump' is 'mutating', which marks a method that changes the struct it is called on,
so the change has to land in a variable, a field or an array element. A fallback supplied with '??'
is not one of those. Check 'local' for null first ('if (local != null)') and write 'local.plain'.

The member being called is looked up on the struct the optional holds, so a read, a class, and a method that changes nothing keep the fallback they had.

A write through ?., local?.plain.value = 2, had the same wrong offer, since a write needs a place too. It drops the fallback where the change lands in a struct value, and keeps it where the value is a class or an array, which change through the reference a fallback carries just as well.

A ?: is no variable, a field or an array element either, so a change on one, (flag ? first?.plain : second?.plain).bump() or the write form, stores the value in a local and checks that instead of offering a null check of the branches that the change could not land in.

…fallback, fixes #156

Co-authored-by: DicyAgent <354226518+DicyAgent@users.noreply.github.com>
@DicyAgent
DicyAgent marked this pull request as ready for review November 9, 2026 08:57

@AlexDicy AlexDicy left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"so a '??' fallback would change only a copy"
Where is this coming from? It's written as if the user attempted it or if it was suggested somewhere else. It's a bit confusing to me.
Does this message respect all of the diagnostic rules?

@DicyAgent
DicyAgent marked this pull request as draft November 11, 2026 20:51
AlexDicy and others added 2 commits November 29, 2026 15:31
Co-authored-by: DicyAgent <354226518+DicyAgent@users.noreply.github.com>
…ck would change

Co-authored-by: DicyAgent <354226518+DicyAgent@users.noreply.github.com>
@DicyAgent

Copy link
Copy Markdown
Collaborator Author

Pushed d9b9f614 and b5dbc286 (head).

The copy clause is gone. The reason now says where the thought comes from - the fallback this refusal is declining - and states it as a property of the change, not of something the reader had written:

Cannot call a member of 'Counter?' because it may be null here. 'holder?.plain' is null whenever
'holder' is null. 'bump' is 'mutating', which marks a method that changes the struct it is called
on, so the change has to land in a variable, a field or an array element. A fallback supplied with
'??' is not one of those. Check 'holder' for null first ('if (holder != null)') and write
'holder.plain'.

Re-checked against the rules, as asked:

  • Wording. "so a '??' fallback would change only a copy" read as the consequence of an act nobody performed. It is replaced by what the change needs, in the compiler's own words for a place ("a variable, a field or an array element", the wording mutation.ens already refuses with), which a fallback is not.
  • Evidence lines. Unchanged: 'Counter?', 'holder?.plain' and 'holder' are the objects' real names, and the fix clause keeps the family's shape ("and write 'holder.plain'"), the one every other guarded refusal uses. Naming the whole call ('holder.plain.bump()') would fork that shared shape; if it is wanted it belongs in an issue over the whole family, not in this message alone.
  • Fix ordering. Both places that carry a reason for a missing fallback now say it before the fix. The two-guard branch of guardedAdvice put it after the check; it is refusal, reason, check now, and the text where a fallback is offered is byte-identical.
  • 'mutating'. Unchanged: the keyword quoted, with the gloss the rest of the family uses ("which marks a method that changes the struct it is called on").

Rule 4 also turned up a defect in the write reason added by e8bb43e9: it claimed a '??' fallback cannot take a write, but a class or an array changes through the reference it carries, and (holder?.box ?? other).size = 1 compiles and changes the object the fallback names. Those writes keep the fallback again (d9b9f614, with a unit test); a struct write still refuses it, and says where its change has to land.

Verified on linux-x64 at b5dbc286 with xmake test -P <worktree>: 634/634 green (selfhost_sema 1188/1188, semacheck 209/209, bootstrap fixpoint, both codegencheck arms).

Co-authored-by: DicyAgent <354226518+DicyAgent@users.noreply.github.com>
@DicyAgent

Copy link
Copy Markdown
Collaborator Author

Round 2 fixes pushed at 8fa0230e.

The join variant of the refusal (rules 3, 5, 21). A ?: is no variable, a field or an array element for a change to land in, so the fix clause the two-guard branch joined with a space was wrong twice over: not a program, and not the fix a careful programmer would make. A ?: value is now stored in a local and checked, which is what the compiler already tells a non-optional join receiver to do:

Cannot call a member of 'Counter?' because it may be null here. 'first?.plain' is null whenever
'first' is null, and 'second?.plain' whenever 'second' is null. 'bump' is 'mutating', which marks a
method that changes the struct it is called on, so the change has to land in a variable, a field or
an array element. A fallback supplied with '??' is not one of those. Store the value of the '?:' in a
local first ('let value = true ? first?.plain : second?.plain;') and check the local for null.

The write form says the same. The whole advice is one sentence per join, so three branches no longer repeat the fix three times. Where a null check of the branches is the fix, a read, the clause now names the join written plainly, and write '(true ? first.plain : second.plain)', which compiles; the plain join is not a place, so it is not offered where the change has to land in one. A join whose type is nullable at more than one level still ends after the why, as it did before.

The PR description no longer quotes the removed so a '??' fallback would change only a copy, and no longer says a write through ?. takes the same fix: a struct write drops the fallback, a class or an array write keeps it. It also covers the ?: case now.

Pinned: a unit test in memberaccess_test.ens for both the call and the write shape, plus a shape each in tests/safe_call_mutating_errors.ens and tests/safe_member_write_errors.ens.

Verified on linux-x64 with a compiler built from this branch: the two shapes and their fixes, the same-guard join, a three-branch join, a read through the same access keeping ??, a binary operator's two-guard clause unchanged, and the multi-level join still ending after the why. Full xmake test 634/634 green, selfhost_sema 1189/1189.

@DicyAgent
DicyAgent marked this pull request as ready for review November 29, 2026 15:26

@AlexDicy AlexDicy left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message is way too long and verbose

@DicyAgent
DicyAgent marked this pull request as draft November 29, 2026 20:47
Co-authored-by: DicyAgent <354226518+DicyAgent@users.noreply.github.com>
@DicyAgent

Copy link
Copy Markdown
Collaborator Author

Pushed 12712fa: merged origin/main (4fb2574) into this branch. Conflicts in memberaccess.ens and nullguidance.ens were resolved onto main's NullJoin/verb signature with this PR's noFallback appended; no message text changed. Full xmake test 636/636 green at 12712fa, bootstrap fixpoint identical, and the round-3 probe shapes print byte-identical text to 8fa0230. Still a draft.

AlexDicy and others added 2 commits November 30, 2026 19:10
Co-authored-by: DicyAgent <354226518+DicyAgent@users.noreply.github.com>
Co-authored-by: DicyAgent <354226518+DicyAgent@users.noreply.github.com>
@DicyAgent

Copy link
Copy Markdown
Collaborator Author

Pushed 19a8503 (on top of 12712fa), still a draft. Round 4 changes:

  • A '?:' or switch of three or more values read through '?.' now gets one check of all three guards, then one write of the whole join. Pinned in nullable_operand_errors.ens and a unit test.
  • The mutating and write refusals through '?.' now state each reason once: 'mutating' gloss, then the place reason with the '??' fallback named in the same sentence.

Full suite is running on this head.

@DicyAgent

Copy link
Copy Markdown
Collaborator Author

Full suite is green at 19a8503 (636/636, bootstrap fixpoint identical, semacheck 211/211). PR stays a draft.

Co-authored-by: DicyAgent <354226518+DicyAgent@users.noreply.github.com>
@DicyAgent

Copy link
Copy Markdown
Collaborator Author

Round 5 pushed: 45e0064 (on 19a8503). PR stays a draft.

  • A join whose branches all read the same place now writes that place once: write 'holder.plain', for both the three-branch and the two-branch read.
  • The '?:' store example is now always shown, so the three-guard mutating join gets ('let value = flag ? first?.plain : (flag ? second?.plain : third?.plain);'). The 40-character cap still applies to switch joins.

Every changed shape is pinned in tests/nullable_operand_errors.ens, tests/safe_call_mutating_errors.ens and nullguidance_test.ens. Full suite 636/636 on the same tree before the commit.

@AlexDicy AlexDicy left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot call 'bump' on 'holder?.plain': a '?.' gives a copy, so changes to it would be lost. Check 'holder' for null first ('if (holder != null)') and write 'holder.plain'.

Co-authored-by: DicyAgent <354226518+DicyAgent@users.noreply.github.com>
@DicyAgent

Copy link
Copy Markdown
Collaborator Author

Pushed 753a1f8: the mutating call through '?.' on a name now reads "Cannot call 'bump' on 'holder?.plain': a '?.' gives a copy, so changes to it would be lost. Check 'holder' for null first ('if (holder != null)') and write 'holder.plain'." Wording as in the review; the join and write refusals are unchanged. PR stays a draft.

AlexDicy and others added 2 commits December 2, 2026 00:02
Co-authored-by: DicyAgent <354226518+DicyAgent@users.noreply.github.com>
@DicyAgent

Copy link
Copy Markdown
Collaborator Author

Pushed de4c9e0 (on 753a1f8, merged with origin/main 44e69e1). Still a draft.

  • The join and write refusals through '?.' or '?:' no longer say 'a '??' fallback is none of those'. They state the copy directly, as the single-holder mutating refusal does: 'A '?:' gives a copy, so changes to it would be lost.' and 'A '?.' gives a copy, so a write to it would be lost.'
  • Full suite 637/637 on this head, selfhost_sema 1198/1198, semacheck 212/212, bootstrap fixpoint identical.

@DicyAgent
DicyAgent marked this pull request as ready for review December 1, 2026 23:34
@AlexDicy
AlexDicy merged commit 431320a into main Dec 1, 2026
12 checks passed
@AlexDicy
AlexDicy deleted the agent/156-optional-chain-mutating-fallback branch December 1, 2026 23:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A mutating call through local?.plain is offered a ?? fallback that the call then refuses

2 participants